/*
 * Project: push-center-service-impl
 * 
 * File Created at 2014-10-11
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.service.core.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;

import com.greenline.guahao.user.model.bo.UserAppDeviceBO;
import com.greenline.guahao.user.service.UserService;
import com.greenline.guahao.user.service.dto.UserDTO;
import com.greenline.guahao.user.service.response.UserResponse;
import com.greenline.guahao.user.service.share.UserAppDeviceService;
import com.greenline.hrs.util.core.bo.page.PageInfo;
import com.greenline.hrs.util.core.bo.response.CodeTable;
import com.greenline.hrs.util.core.bo.response.Response;

/**
 * 用户管理
 * 
 * @author weirui.shenwr
 * @date 2014-10-11
 * @version V1.0
 */
public class UserManager {
    @Resource
    private UserAppDeviceService userAppDeviceService;
    @Resource
    private UserService guahaoUserService;
    public static final Integer DEFAULT_PAGE_SIZE = 100;

    /**
     * @Desc 根据用户id获取设备列表信息
     * @author zhaoli
     * @param userId 用户id
     * @return List<AppUserInfo>
     */
    public List<AppUserInfo> listByUserId(Long userId) {
        List<AppUserInfo> result = null;
        List<Long> userIds = new ArrayList<Long>();
        userIds.add(userId);
        Response<List<UserAppDeviceBO>> response = userAppDeviceService.listByUserIds(userIds, null);
        if (CodeTable.SUCCESS.getCode() == response.getCode()) {
            List<UserAppDeviceBO> users = response.getData();
            if (!CollectionUtils.isEmpty(users)) {
                result = new ArrayList<AppUserInfo>();
                for (UserAppDeviceBO userAppDeviceBO : users) {
                    AppUserInfo appUserInfo = new AppUserInfo();
                    appUserInfo.setAppId(String.valueOf(userAppDeviceBO.getAppId()));
                    appUserInfo.setUserId(userAppDeviceBO.getUid());
                    appUserInfo.setDeviceToken(userAppDeviceBO.getDeviceToken());
                    result.add(appUserInfo);
                }

            }
        }
        return result;
    }

    /**
     * @Desc 根据用户id和应用id获取设备列表信息
     * @author zhaoli
     * @param userId 用户id
     * @param appId 应用id
     * @return AppUserInfo
     */
    public AppUserInfo getAppUserInfo(Long userId, String appId) {
        AppUserInfo appUserInfo = null;
        List<Long> userIds = new ArrayList<Long>();
        userIds.add(userId);
        Response<List<UserAppDeviceBO>> response = userAppDeviceService.listByUserIds(userIds, appId);
        if (CodeTable.SUCCESS.getCode() == response.getCode()) {
            List<UserAppDeviceBO> users = response.getData();
            if (!CollectionUtils.isEmpty(users)) {
                UserAppDeviceBO user = users.get(0);
                appUserInfo = new AppUserInfo();
                appUserInfo.setAppId(String.valueOf(user.getAppId()));
                appUserInfo.setUserId(user.getUid());
                appUserInfo.setDeviceToken(user.getDeviceToken());
            }
        }
        // 测试
        // appUserInfo = new AppUserInfo();
        // appUserInfo.setAppId("wx266d52f8ac9b4ac9");
        // appUserInfo.setUserId(11791781l);
        // appUserInfo.setDeviceToken("oLTB2uKC4PJVeaZecFtsNnn6v3Wk");

        return appUserInfo;
    }

    /**
     * 根据用户id获取用户联系信息
     * 
     * @param userId
     * @return UserConnact
     */
    public UserConnact getUserConnact(Long userId) {
        UserConnact userConnact = null;
        UserResponse<UserDTO> response = guahaoUserService.getUserByUserId(userId);
        if (response.isSuccess() && response.getData() != null) {
            userConnact = new UserConnact();
            UserDTO userDTO = response.getData();
            userConnact.setEmail(userDTO.getEmail());
            userConnact.setMobile(userDTO.getMobile());
            userConnact.setUserId(userId);
        }
        return userConnact;
    }

    /**
     * @author shuai.zuo <br/>
     * @description 根据app_id 拿到所有uid
     * @param appId
     * @return
     */
    public List<Long> getUserListByAppId(String appId, Integer pageNo) {

        UserAppDeviceBO bo = new UserAppDeviceBO();
        bo.setAppId(appId);
        PageInfo<UserAppDeviceBO> p = new PageInfo<UserAppDeviceBO>();
        p.setPageSize(DEFAULT_PAGE_SIZE);
        p.setPageNo(pageNo);
        Response<PageInfo<UserAppDeviceBO>> userListpage = userAppDeviceService.page(bo, p);
        PageInfo<UserAppDeviceBO> users = userListpage.getData();
        List<UserAppDeviceBO> allUsers = users.getList();
        List<Long> useridSet = new ArrayList<Long>();
        for (UserAppDeviceBO tmp : allUsers) {
            useridSet.add(tmp.getUid());
        }

        // test
        // List<Long> useridSet = new ArrayList<Long>();
        // if (pageNo > 20)
        // return useridList;
        // for (int i = 0; i < DEFAULT_PAGE_SIZE; i++) {
        // useridSet.add((long) (pageNo * DEFAULT_PAGE_SIZE + i));
        // }
        return useridSet;
    }
}
